import java.util.*;

class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
public class Text {

    //51
    List<List<String>> ret = new ArrayList<>();
    private boolean isValid(int col,int row,int n,char[][] chessboard) {
        for(int i = 0;i < n; i++) {
            if(chessboard[i][col] == 'Q') {
                return false;
            }
        }
        for(int i = row-1,j = col-1;i>=0 && j>=0;i--,j--) {
            if(chessboard[i][j] == 'Q') {
                return false;
            }
        }
        for(int i = row-1,j = col+1;i>=0 && j<n;i--,j++) {
            if(chessboard[i][j] == 'Q') {
                return false;
            }
        }
        return true;
    }
    public void func(int n,int row,char[][] chessboard) {
        if(n == row) {
            List<String> s = new ArrayList<>();
            for(char[] str:chessboard) {
                s.add(String.valueOf(str));
            }
            ret.add(s);
            return;
        }
        for(int i = 0; i < n; i++ ) {
            if(isValid(i,row,n,chessboard)) {
                chessboard[row][i] = 'Q';
                func(n,row+1,chessboard);
                chessboard[row][i] = '.';
            }
        }

    }
    public List<List<String>> solveNQueens(int n) {
        char[][] chessboard = new char[n][n];
        for(char[] c:chessboard) {
            Arrays.fill(c,'.');
        }
        func(n,0,chessboard);
        return ret;
    }
    private boolean isvoild(int row,int col,char k,char[][] board) {
        for(int n = 0;n < 9;n++) {
            if(board[n][col] == k) {
                return false;
            }
        }
        for(int n = 0;n < 9;n++) {
            if(board[row][n] == k) {
                return false;
            }
        }
        int startrow = (row/3)*3;
        int startcol = (col/3)*3;
        for(int n = startrow;n < startrow+3;n++) {
            for(int m = startcol;m < startcol+3;m++) {
                if(board[n][m] == k) {
                    return false;
                }
            }
        }
        return true;
    }
    //37
    public boolean func(char[][] board) {
        for(int i = 0; i < board.length; i++) {
            for(int j = 0; j < board[0].length; j++) {
                if(board[i][j] == '.') {
                    for(char k = '1'; k <= '9'; k++) {
                        if(isvoild(i,j,k,board)){
                            board[i][j] = k;
                            if(func(board)) {
                                return true;
                            }
                            board[i][j] = '.';
                        }
                    }
                    return false;
                }
            }
        }
        return true;
    }
    public void solveSudoku(char[][] board) {
        func(board);
    }
    public ListNode reverseList(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        head.next = null;
        while(cur != null) {
            ListNode curnext = cur.next;
            cur.next = prev;
            prev = cur;
            cur = curnext;
        }
        return prev;
    }
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            while(cur != null && cur.val == prev.val) {
                prev.next = cur.next;
                cur = cur.next;
            }
            prev = cur;
            if(cur != null) {
                cur = cur.next;
            }
        }
        return head;
    }
}
